package com.example.lenovo.wechat_view;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> {
    private List<Chat> mChatList;
    private Context context;

    static class ViewHolder extends RecyclerView.ViewHolder {
        View chatView;
        TextView chatName;
        ImageView chatImg;

        public ViewHolder(View view) {
            super(view);
            chatView = view;
            chatName = view.findViewById(R.id.chat_name);
            chatImg = view.findViewById(R.id.chat_img);
        }
    }

    public ChatAdapter(Context context, List<Chat> chatList) {
        this.context = context;
        mChatList = chatList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.chat_item, null);
        final ViewHolder holder = new ViewHolder(view);

        holder.chatView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                Toast.makeText(v.getContext(), "test", Toast.LENGTH_SHORT).show();
            }
        });


        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Chat chat = mChatList.get(position);
        holder.chatName.setText(chat.getName());
        holder.chatImg.setImageResource(chat.getImgPath());
    }

    @Override
    public int getItemCount() {
        return mChatList.size();
    }


}
